<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="填报企业" prop="company">
        <el-input
          v-model="queryParams.company"
          placeholder="请输入填报企业"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="信用代码" prop="unifiedSocialCode">
        <el-input
          v-model="queryParams.unifiedSocialCode"
          placeholder="请输入社会统一信用代码"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="企业类型" prop="companyType">
        <el-select v-model="queryParams.companyType" placeholder="企业类型" @change="getList" clearable>
          <el-option
            v-for="dict in dict.type.business_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"

          />
        </el-select>
      </el-form-item>
      <el-form-item label="数据状态" prop="dataStatus">
        <el-select v-model="queryParams.dataStatus" placeholder="数据状态" @change="getList" clearable>
          <el-option
            v-for="dict in dict.type.data_status_examine_v3"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"

          />
        </el-select>
      </el-form-item>

      <el-form-item label="创建人" prop="createBy" v-show="isFoldQuery">
        <el-input
          v-model="queryParams.createBy"
          placeholder="请输入创建人"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="创建时间" v-show="isFoldQuery">
        <el-date-picker
          v-model="createDateRange"
          style="width: 240px"
          value-format="yyyy-MM-dd HH:mm:ss"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          :default-time="['00:00:00', '23:59:59']"
        ></el-date-picker>
      </el-form-item>
      <el-form-item label="修改人" prop="updateBy" v-show="isFoldQuery">
        <el-input
          v-model="queryParams.updateBy"
          placeholder="请输入修改人"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="修改时间" v-show="isFoldQuery">
        <el-date-picker
          v-model="updateDateRange"
          style="width: 240px"
          value-format="yyyy-MM-dd HH:mm:ss"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          :default-time="['00:00:00', '23:59:59']"
        ></el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
                <el-button
          plain
          :icon="isFoldQuery ? 'el-icon-caret-top' : 'el-icon-caret-bottom'"
          size="mini"
          @click="foldQuery"
        >
          {{ isFoldQuery ? '折叠' : '展开' }}
        </el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['dataBase:reportObj:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['dataBase:reportObj:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table border v-loading="loading" :data="reportObjList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="主键id" align="center" prop="id" v-if="false"/>
      <el-table-column label="填报企业" align="center" prop="company" fixed width="200"/>
      <el-table-column label="企业类型" align="center" prop="companyType" fixed width="120">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.business_type" :value="scope.row.companyType"/>
        </template>
      </el-table-column>
      <el-table-column label="社会统一信用代码" align="center" prop="unifiedSocialCode" fixed width="300"/>
      <el-table-column label="联系人" align="center" prop="contact"  width="200"/>
      <el-table-column label="联系方式" align="center" prop="phone"  width="200"/>
      <el-table-column label="备注" align="center" prop="remark"  width="200"/>
      <el-table-column label="创建人" align="center" prop="createBy" width="220"/>
      <el-table-column label="创建时间" align="center" prop="createTime" width="220"/>
      <el-table-column label="最后修改人" align="center" prop="updateBy" width="220"/>
      <el-table-column label="修改时间" align="center" prop="updateTime" width="220"/>
      <el-table-column label="数据状态" align="center" prop="dataStatus" width="120" fixed="right">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.data_status_examine_v3" :value="scope.row.dataStatus"/>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" fixed="right" width="200">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit-outline"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['dataBase:reportObj:edit']"
            v-if="scope.row.dataStatus ===0 || scope.row.dataStatus ===2 || scope.row.dataStatus ===4"
          >修改
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-document-checked"
            @click="handleUp(scope.row)"
            v-hasPermi="['dataBase:reportObj:edit']"
            v-if="scope.row.dataStatus ===0 || scope.row.dataStatus ===2"
          >提交
          </el-button>

          <!--          <el-button-->
          <!--            size="mini"-->
          <!--            type="text"-->
          <!--            icon="el-icon-close"-->
          <!--            @click="handleUpdate(scope.row)"-->
          <!--            v-hasPermi="['dataBase:reportObj:edit']"-->
          <!--            v-if="scope.row.dataStatus ===1"-->
          <!--          >撤回</el-button>-->
          <el-button
            size="mini"
            type="text"
            icon="el-icon-remove-outline"
            @click="handleStop(scope.row)"
            v-hasPermi="['dataBase:reportObj:edit']"
            v-if="scope.row.dataStatus ===3"
          >停用
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-circle-check"
            @click="handleStart(scope.row)"
            v-hasPermi="['dataBase:reportObj:edit']"
            v-if="scope.row.dataStatus ===4"
          >启用
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['dataBase:reportObj:remove']"
            v-if="scope.row.dataStatus ===0 || scope.row.dataStatus ===4"
          >删除
          </el-button>


<!--          <el-button-->
<!--            size="mini"-->
<!--            type="text"-->
<!--            icon="el-icon-check"-->
<!--            @click="handleReviewCheck(scope.row)"-->
<!--            v-hasPermi="['dataBase:reportObj:review']"-->
<!--            v-if="scope.row.dataStatus ===1"-->
<!--          >审核通过-->
<!--          </el-button>-->
<!--          <el-button-->
<!--            size="mini"-->
<!--            type="text"-->
<!--            icon="el-icon-close"-->
<!--            @click="handleReviewClose(scope.row)"-->
<!--            v-hasPermi="['dataBase:reportObj:review']"-->
<!--            v-if="scope.row.dataStatus ===1"-->
<!--          >审核驳回-->
<!--          </el-button>-->
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改填报对象对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="填报企业" prop="company">
          <el-input v-model="form.company" placeholder="请输入填报企业" />
        </el-form-item>
        <el-form-item label="信用代码" prop="unifiedSocialCode">
          <el-input v-model="form.unifiedSocialCode" placeholder="请输入社会统一信用代码" />
        </el-form-item>
        <el-form-item label="企业类型" prop="companyType">
          <el-select v-model="form.companyType" placeholder="请选择企业类型" style="width:100%">
            <el-option
              v-for="dict in dict.type.business_type"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="联系人" prop="contact">
          <el-input v-model="form.contact" placeholder="请输入联系人" />
        </el-form-item>
        <el-form-item label="联系方式" prop="phone">
          <el-input v-model="form.phone" placeholder="请输入联系方式" />
        </el-form-item>
<!--        <el-form-item label="备注" prop="remark">-->
<!--          <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" />-->
<!--        </el-form-item>-->
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button :loading="buttonLoading" type="primary" @click="submitForm2" v-if="form.dataStatus !== 1">提 交
        </el-button>
        <el-button :loading="buttonLoading" type="primary" @click="submitForm1" v-if="form.dataStatus !== 1">保 存
        </el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listReportObj, getReportObj, delReportObj, addReportObj, updateReportObj } from "@/api/dataBase/reportObj";

export default {
  name: "ReportObj",
  dicts: ['business_type', 'data_status_examine_v3'],
  data() {
    return {
      isFoldQuery:false,
      // 日期范围
      createDateRange: [],
      updateDateRange: [],
      // 按钮loading
      buttonLoading: false,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 填报对象表格数据
      reportObjList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        company: undefined,
        companyType: undefined,
        unifiedSocialCode: undefined,
        contact: undefined,
        phone: undefined,
        createBy: undefined,
        updateBy: undefined,
        dataStatus: undefined
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        id: [
          { required: true, message: "主键id不能为空", trigger: "blur" }
        ],
        company: [
          { required: true, message: "填报企业不能为空", trigger: "blur" }
        ],
        companyType: [
          { required: true, message: "填报企业分类不能为空", trigger: "change" }
        ],
        unifiedSocialCode: [
          { required: true, message: "社会统一信用代码不能为空", trigger: "blur" }
        ],
        contact: [
          { required: true, message: "联系人不能为空", trigger: "blur" }
        ],
        phone: [
          {
            pattern: /^1[3|4|5|6|7|8|9][0-9]\d{8}$/,
            message: "请输入正确的手机号码",
            trigger: "blur"
          }
        ],
        remark: [
          { required: true, message: "备注不能为空", trigger: "blur" }
        ],
        dataStatus: [
          { required: true, message: "数据状态不能为空", trigger: "change" }
        ]
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询填报对象列表 */
    getList() {
      this.loading = true;
      this.queryParams.params = {}
      if (null != this.createDateRange && '' != this.createDateRange) {
        this.queryParams.params['beginCreateTime'] = this.createDateRange[0]
        this.queryParams.params['endCreateTime'] = this.createDateRange[1]
      }
      if (null != this.updateDateRange && '' != this.updateDateRange) {
        this.queryParams.params['beginUpdateTime'] = this.updateDateRange[0]
        this.queryParams.params['endUpdateTime'] = this.updateDateRange[1]
      }
      listReportObj(this.queryParams).then(response => {
        this.reportObjList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        company: undefined,
        companyType: undefined,
        unifiedSocialCode: undefined,
        contact: undefined,
        phone: undefined,
        delFlag: undefined,
        createBy: undefined,
        createTime: undefined,
        updateBy: undefined,
        updateTime: undefined,
        remark: undefined,
        dataStatus: undefined
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      console.log("点击重置按钮")
      this.createDateRange = []
      this.updateDateRange = []
      this.resetForm("queryForm");
      this.handleQuery();
    },
    foldQuery() {
      this.isFoldQuery = !this.isFoldQuery;
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加填报对象";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.loading = true;
      this.reset();
      const id = row.id || this.ids
      getReportObj(id).then(response => {
        this.loading = false;
        this.form = response.data;
        this.open = true;
        this.title = "修改填报对象";
      });
    },
    /** 提交按钮 */
    submitForm1() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.buttonLoading = true;
          this.form.dataStatus = 0
          if (this.form.id != null) {
            updateReportObj(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            }).finally(() => {
              this.buttonLoading = false;
            });
          } else {
            addReportObj(this.form).then(response => {
              this.$modal.msgSuccess("保存成功");
              this.open = false;
              this.getList();
            }).finally(() => {
              this.buttonLoading = false;
            });
          }
        }
      });
    },
    submitForm2() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.buttonLoading = true;
          this.form.dataStatus = 1
          if (this.form.id != null) {
            updateReportObj(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            }).catch((error)=>{
              this.form.dataStatus = 0
            }).finally(() => {
              this.buttonLoading = false;
            });
          } else {
            addReportObj(this.form).then(response => {
              this.$modal.msgSuccess("提交成功");
              this.open = false;
              this.getList();
            }).catch((error)=>{
              this.form.dataStatus = 0
            }).finally(() => {
              this.buttonLoading = false;
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除填报对象编号为"' + ids + '"的数据项？').then(() => {
        this.loading = true;
        return delReportObj(ids);
      }).then(() => {
        this.loading = false;
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      }).finally(() => {
        this.loading = false;
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('dataBase/reportObj/export', {
        ...this.queryParams
      }, `reportObj_${new Date().getTime()}.xlsx`)
    },



    handleUp(row) {
      this.$modal.confirm('是否确认提交？').then(() => {
        this.loading = true
        row.dataStatus = 3
        return updateReportObj(row)
      }).then(() => {
        this.loading = false
        this.getList()
        this.$modal.msgSuccess('提交成功')
      }).catch(() => {
        this.getList()
      }).finally(() => {
        this.loading = false
      })
    },
    handleStart(row) {
      this.$modal.confirm('是否确认启用？').then(() => {
        this.loading = true
        row.dataStatus = 3
        return updateReportObj(row)
      }).then(() => {
        this.loading = false
        this.getList()
        this.$modal.msgSuccess('启用成功')
      }).catch(() => {
      }).finally(() => {
        this.loading = false
      })
    },
    handleStop(row) {
      this.$modal.confirm('是否确认停用？').then(() => {
        this.loading = true
        row.dataStatus = 4
        return updateReportObj(row)
      }).then(() => {
        this.loading = false
        this.getList()
        this.$modal.msgSuccess('停用成功')
      }).catch(() => {
      }).finally(() => {
        this.loading = false
      })
    },
    handleReviewCheck(row) {
      this.$modal.confirm('是否确认通过审核？').then(() => {
        this.loading = true
        row.dataStatus = 3
        return updateReportObj(row)
      }).then(() => {
        this.loading = false
        this.getList()
        this.$modal.msgSuccess('审核通过成功')
      }).catch(() => {
        this.getList()
      }).finally(() => {
        this.loading = false
      })
    },
    handleReviewClose(row) {
      this.$modal.confirm('是否确认驳回？').then(() => {
        this.loading = true
        row.dataStatus = 2
        return updateReportObj(row)
      }).then(() => {
        this.loading = false
        this.getList()
        this.$modal.msgSuccess('驳回成功')
      }).catch(() => {
        this.getList()
      }).finally(() => {
        this.loading = false
      })
    },
  }
};
</script>
